﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Concrete;
using System.ComponentModel;
using SportsStore.WebUI.Models;

namespace SportsStore.WebUI.Controllers
{
	public class ProductsController : Controller
	{
		public int PageSize = 2;
		private IProductsRepository productsRepository;

		public ProductsController(IProductsRepository productsRepository)
		{
			this.productsRepository = productsRepository;
			//this.productsRepository = new FakeProductsRepository();
		}

		public ViewResult List(string category, [DefaultValue(1)] int page)
		{
			//var productsToShow = productsRepository.Products;

			var productsToShow = (category == null)
				? productsRepository.Products
				: productsRepository.Products.Where(x => x.Category == category);

			var viewModel = new ProductsListViewModel
			{
				Products = productsToShow.Skip((page - 1) * PageSize).Take(PageSize).ToList(),
				PagingInfo = new PagingInfo
				{
					CurrentPage = page,
					ItemsPerPage = PageSize,
					TotalItems = productsToShow.Count()
				},
				CurrentCategory = category
			};

			return View(viewModel);
		}
	}
}
